package com.dataextend.controller;

import com.dataextend.entity.*;
import com.dataextend.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "角色菜单接口")
@RestController
@RequestMapping("/api/role/menus")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class RootRoleMenusController extends RootBaseApiController {

    @Autowired
    private RoleMenusService roleMenusService;
    @Autowired
    private MenusService menusService;

    @ApiOperation("添加")
    @RequiresPermissions("role:menus:add")
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody RoleMenusRequest roleMenusRequest) {
        Long roleId = roleMenusRequest.getRoleId();
        List<Long> menusIds = roleMenusRequest.getMenusId();

        for (Long menusId : menusIds) {
            RoleMenusEntity roleMenusEntity = new RoleMenusEntity()
                    .setRoleId(roleId)
                    .setMenusId(menusId);

            MenusEntity menusEntity = menusService.queryById(menusId);
            if (menusEntity.getParentId() != 0) {
                List<RoleMenusEntity> list = roleMenusService.list(new RoleMenusEntity().setRoleId(roleId).setMenusId(menusEntity.getParentId()));

                if (list.size() == 0) {
                    roleMenusService.add(new RoleMenusEntity().setRoleId(roleId).setMenusId(menusEntity.getParentId()));
                }
            }

            roleMenusService.add(roleMenusEntity);
        }

        return resp(200, "添加成功", "success");
    }

    @ApiOperation("修改")
    @RequiresPermissions("role:menus:update")
    @PostMapping("/update")
    public Map<String, Object> update(@RequestBody RoleMenusRequest roleMenusRequest) {
//        RoleMenusEntity roleMenusEntity = new RoleMenusEntity()
//                .setId(id)
//                .setRoleId(roleId)
//                .setMenusId(menusId);
//        roleMenusService.update(roleMenusEntity)

        Long roleId = roleMenusRequest.getRoleId();
        roleMenusService.deleteRoleId(roleId);
        for (long menusId : roleMenusRequest.getMenusId()){
            roleMenusService.addRoleMenus(roleId, menusId);
        }

        return resp(200, "修改成功", "success");
    }

    @ApiOperation("删除")
    @RequiresPermissions("role:menus:delete")
    @PostMapping("/delete")
    public Map<String, Object> deleteById(@RequestBody RoleMenusDeleRequest roleMenusDeleRequest) {
        for (Long id : roleMenusDeleRequest.getIds()){
            roleMenusService.delete(id);
        }
        return resp(200, "删除成功");
    }

    @ApiOperation("根据id查询")
    @GetMapping("/query")
    public Map<String, Object> queryById(@RequestParam(value = "id", required = false) Long id) {
        return resp(200, "查询成功", roleMenusService.queryById(id));
    }

    @ApiOperation("查询所有")
    @GetMapping("/list")
    public Map<String, Object> list(@RequestParam(value = "id", required = false) Long id,
                                    @RequestParam(value = "roleId", required = false) Long roleId,
                                    @RequestParam(value = "menusId", required = false) Long menusId) {
        RoleMenusEntity roleMenusEntity = new RoleMenusEntity()
                .setId(id)
                .setRoleId(roleId)
                .setMenusId(menusId);

        return resp(200, "查询所有成功", roleMenusService.list(roleMenusEntity));
    }
}
